import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login.vue'
import home from '../components/Home.vue'
import welcome from '../components/Welcome.vue'
import users from '../components/Users.vue'
import rights from '../components/power/Rights.vue'
import roles from '../components/power/Roles.vue'
import categories from '../components/goods/Cate.vue'
import params from '../components/goods/Params.vue'
import goods from '../components/goods/List.vue'
import add from '../components/goods/Add.vue'
import order from '../components/order/Order.vue'
import reports from '../components/report/Report.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [{ path: '/welcome', component: welcome },
      { path: '/users', component: users },
      { path: '/rights', component: rights },
      { path: '/roles', component: roles },
      { path: '/categories', component: categories },
      { path: '/params', component: params },
      { path: '/params', component: params },
      { path: '/goods', component: goods },
      { path: '/goods/add', component: add },
      { path: '/orders', component: order },
      { path: '/reports', component: reports }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 挂在路由导航收尾
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行， next()放行， next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果token不为空，直接跳转到login
  if (!tokenStr) return next('/login')
  next()
})
export default router
